//
//  CardInfoTableViewCell.m
//  QRMaster
//
//  Created by myApple on 15/6/16.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "CardInfoTableViewCell.h"
#import "OpertionPlist.h"
#import "PromptInfoView.h"

@interface CardInfoTableViewCell()<UIGestureRecognizerDelegate>
{
    UIImageView * LeftImgView;
    UILabel * roomLabel;
    UILabel * nameLabel;
    UIView * leftView;
    UIButton * faveritBtn;
    UISwipeGestureRecognizer * leftSwipeGestureRecognizer;
    UISwipeGestureRecognizer * rightSwipeGestureRecognizer;
}

@end

static NSInteger swipeCount = 0;

@implementation CardInfoTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self prepareView];
    }
    return self;
}

-(void)prepareView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = 0;
    float y = 0;
    float width = ScreenWidth + 200;
    float height = 60;
    
    leftView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    leftView.userInteractionEnabled = YES;
    leftView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:leftView];
    
    leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipes:)];
    rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipes:)];
    leftSwipeGestureRecognizer.delegate = self;
    rightSwipeGestureRecognizer.delegate = self;
    leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    
    [leftView addGestureRecognizer:leftSwipeGestureRecognizer];
    [leftView addGestureRecognizer:rightSwipeGestureRecognizer];
    
    
    x = 10;
    y = 10;
    width = 40;
    height = 40;
    self.backgroundColor = RGBA(237, 238, 241, 1);
    LeftImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    LeftImgView.image = [UIImage imageNamed:@"ic_card"];
    [leftView addSubview:LeftImgView];
    
    
    x += width + 10;
    y = 20;
    width = ScreenWidth - x;
    height = 20;
    roomLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    roomLabel.font = [UIFont systemFontOfSize:14.0];
    [leftView addSubview:roomLabel];
    
    
//    y += height + 2;
//    height = 15;
//    nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
//    nameLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
//    nameLabel.font = [UIFont systemFontOfSize:12.0];
//    [leftView addSubview:nameLabel];
    
    //收藏
    faveritBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    faveritBtn.frame = CGRectMake(ScreenWidth, 0, 60, 60);
    [faveritBtn setBackgroundColor:RGBA(125, 194, 52, 1)];
    [faveritBtn addTarget:self action:@selector(faveritclick) forControlEvents:UIControlEventTouchUpInside];
    [leftView addSubview:faveritBtn];
    _faveritImgView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 30, 30)];
    _faveritImgView.userInteractionEnabled = YES;
    [faveritBtn addSubview:_faveritImgView];
    UITapGestureRecognizer * tapFaveritBtn = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(faveritclick)];
    [faveritBtn addGestureRecognizer:tapFaveritBtn];
    
    //查看时间
    UIButton * timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    timeBtn.frame = CGRectMake(60 + ScreenWidth , 0, 60, 60);
    [timeBtn setBackgroundColor:RGBA(237, 238, 241, 1)];
    [timeBtn addTarget:self action:@selector(timeClick) forControlEvents:UIControlEventTouchUpInside];
    [leftView addSubview:timeBtn];
    _timeImgView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 30, 30)];
    _timeImgView.userInteractionEnabled = YES;
    [timeBtn addSubview:_timeImgView];
    UITapGestureRecognizer * tapTimeBtn = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(timeClick)];
    [timeBtn addGestureRecognizer:tapTimeBtn];
    
}

-(void)setCardStyle:(NSInteger)cardStyle
{
    _cardStyle = cardStyle;
    if (cardStyle == 1) {
         LeftImgView.image = [UIImage imageNamed:@"ic_zcard"];
    }
    else
    {
        LeftImgView.image = [UIImage imageNamed:@"ic_card"];
    }
}

-(void)setRoomName:(NSString *)roomName
{
    _roomName = roomName;
    roomLabel.text = roomName;
}

-(void)setBulidingName:(NSString *)bulidingName
{
    _bulidingName = bulidingName;
}

-(void)setFloorName:(NSString *)floorName
{
    _floorName = floorName;
    nameLabel.text = [NSString stringWithFormat:@"%@%@",_bulidingName,_floorName];
}

-(void)setRoomId:(NSString *)roomId
{
    _roomId = roomId;
    if ([_roomId integerValue] < 0) {
        faveritBtn.hidden = YES;
    }
    else
    {
        faveritBtn.hidden = NO;
    }
}

-(void)setBulidingId:(NSString *)bulidingId
{
    _bulidingId = bulidingId;
}

-(void)setFloorId:(NSString *)floorId
{
    _floorId = floorId;
}

-(void)setCommunityid:(NSString *)communityid
{
    _communityid = communityid;
}

-(void)setIndex:(int)Index
{
    _Index = Index;
}

-(void)setReloadView:(BOOL)reloadView
{
    _reloadView = reloadView;
    if (reloadView) {
        swipeCount = 0;
    }
}

#pragma mark -- 解决tableview中手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    if ([NSStringFromClass([touch.view class]) isKindOfClass:[UIButton class]]) {
        return NO;
    }
    return  YES;
}

-(void)handleLeftSwipes:(UISwipeGestureRecognizer *)swipe
{
    if (leftView.frame.origin.x != 0) {
        return;
    }
    if (swipeCount !=0) {
        if (_delegate && [_delegate respondsToSelector:@selector(closeSwipeView)]) {
            [_delegate closeSwipeView];
            swipeCount = 0;
            return;
        }
    }
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft ) {
        [UIView animateWithDuration:0.5 animations:^{
            leftView.frame = CGRectMake(-120, leftView.frame.origin.y, leftView.frame.size.width, leftView.frame.size.height);
        } completion:^(BOOL finished) {
            swipeCount ++;
            if (_delegate && [_delegate respondsToSelector:@selector(sendControllerHavePan:cellView:)]) {
                [_delegate sendControllerHavePan:swipeCount cellView:leftView];
            }
        }];
        
    }
}

-(void)handleRightSwipes:(UISwipeGestureRecognizer *)swipe
{
    if (leftView.frame.origin.x != -120) {
        return;
    }
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight ) {
        [UIView animateWithDuration:0.5 animations:^{
            leftView.frame = CGRectMake(0, leftView.frame.origin.y, leftView.frame.size.width, leftView.frame.size.height);
        } completion:^(BOOL finished) {
            swipeCount --;
            if (_delegate && [_delegate respondsToSelector:@selector(sendControllerHavePan:cellView:)]) {
                [_delegate sendControllerHavePan:swipeCount cellView:leftView];
            }
        }];
        
    }
}

-(void)faveritclick
{
    if (_delegate && [_delegate respondsToSelector:@selector(sendControllerClickFaverit: cardInfoCell:)]) {
        [_delegate sendControllerClickFaverit:_Index cardInfoCell:self];
    }
    swipeCount = 0;
}

-(void)timeClick
{
    if ([_roomId isEqualToString:[NSString stringWithFormat:@"-%@",_communityid]]) {
        [PromptInfoView showMessage:NSLocalizedString(@"allTimeOpen", nil)];
        return;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(sendShowTimeClick:)]) {
        [_delegate sendShowTimeClick:_roomId];
    }
    swipeCount = 0;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
